/**
 *    Copyright (C) 2008  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_TOOLS_TYPES_LIST_INDEX_HPP
#define	_CUBEFS_CORE_TOOLS_TYPES_LIST_INDEX_HPP

#include <cubefs/core/tools/types/typelist.hpp>

namespace cubefs    {
namespace tools     {

template <typename Item_> struct DefaultItemProvider {
    typedef Item_ Item;
};

template <typename Item, typename List, Int Idx = 0> struct IndexOfTool;

template <typename Item, typename Tail, Int Idx>
struct IndexOfTool<Item, TypeList<Item, Tail>, Idx> {
    static const Int Value = Idx;
};

template <typename Item, typename Head, typename Tail, Int Idx>
struct IndexOfTool<Item, TypeList<Head, Tail>, Idx> {
    static const Int Value = IndexOfTool<Item, Tail, Idx + 1>::Value;
};

template <typename Item, Int Idx>
struct IndexOfTool<Item, NullType, Idx> {
    static const Int Value = -1; //not found
};


template <Int Idx, typename List, Int Counter = 0> struct SelectByIndexTool;

template <Int Idx, typename Head, typename Tail>
struct SelectByIndexTool<Idx, TypeList<Head, Tail>, Idx> {
    typedef Head                                                                Result;
};

template <Int Idx, typename Head, typename Tail, Int Counter>
struct SelectByIndexTool<Idx, TypeList<Head, Tail>, Counter> {
    typedef typename SelectByIndexTool<Idx, Tail, Counter + 1>::Result          Result;
};

template <Int Value> struct ListIndexOutOfRange {};

template <Int Idx, Int Counter>
struct SelectByIndexTool<Idx, NullType, Counter> {
    typedef ListIndexOutOfRange<Idx>                                            Result;
};

}
}

#endif	/* _CUBEFS_CORE_TOOLS_TYPES_LIST_INDEX_HPP */